mod example_capnp {
    include!(concat!(env!("OUT_DIR"), "/example_capnp.rs"));
}

use capnp::message::Builder;
use capnp::message::ReaderOptions;
use capnp::{serialize, serialize_packed};

pub fn write_point() -> ::capnp::Result<()> {
    let mut message = ::capnp::message::Builder::new_default();
    {
        let mut point = message.init_root::<example_capnp::point::Builder>();
        let mut p = point.reborrow();
        p.set_x(1.0);
        p.set_y(2.0);
    }
    serialize_packed::write_message(&mut ::std::io::stdout(), &message)
}

pub fn serialize_point(x: f32, y: f32) -> Vec<u8> {
    let mut msg = Builder::new_default();

    let mut point = msg.init_root::<example_capnp::point::Builder>();
    point.set_x(x);
    point.set_y(y);

    //let start = Instant::now();
    let data = serialize::write_message_to_words(&msg);
    //let duration = start.elapsed();
    //println!("CAPNP SE: duration: {:?}, len: {}", duration, data.len());
    data
}

pub fn deserialize_point(data: &[u8]) -> (f32, f32) {
    //    let start = Instant::now();

    let reader = serialize::read_message(data, ReaderOptions::new()).expect("can create reader");
    let cat = reader
        .get_root::<example_capnp::point::Reader>()
        .expect("can deserialize cat");
    (cat.get_x(), cat.get_y())
    //  let duration = start.elapsed();
    //   println!(
    //       "CAPNP DE: {} duration: {:?}",
    //       cat.get_name().unwrap(),
    //       duration
    //   );
}
